package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.PostsDomain;
import com.xingkeduo.pojo.json.PostsJson;
import com.xingkeduo.pojo.query.PostsQuery;
import com.xingkeduo.pojo.table.PostsTable;
import com.xingkeduo.util.tool.ReturnUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;

/**
 * 职位管理
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Slf4j
@RestController
@RequestMapping(CMS_URL_PREFIX +"/posts")
public class PostsController extends InitController {

    @Autowired
    private PostsDomain postsDomain;

    //查看
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public String doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            long id = PostsJson.createQueryForPosts(urlParam);
            PostsTable postsTable = postsDomain.getPostsById(id);
            return returnUtil.getSuccess(PostsJson.toJson(postsTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String doPost(@RequestBody @Valid PostsTable postsVo , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            postsVo.setCreatedBy(this.getOperator(request));
            log.info("添加岗位接口请求参数:{}", JSON.toJSONString(postsVo));
            int posts = postsDomain.createPosts(postsVo);
            log.info("添加岗位接口返回值:{}", JSON.toJSONString(posts));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam, @RequestBody @Valid PostsTable postsVo  , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            PostsTable postsTable = PostsJson.createTableForModifyPosts(urlParam,postsVo);
            postsVo.setUpdatedBy(this.getOperator(request));
            log.info("修改岗位接口请求参数:{}", JSON.toJSONString(postsVo));
            int posts = postsDomain.modifyPosts(postsTable);
            log.info("修改岗位接口返回值:{}", JSON.toJSONString(posts));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            long id  = PostsJson.createQueryForPosts(urlParam);
            postsDomain.delPosts(id);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    @RequestMapping(value = "/lists/{urlParam:.+}", method = RequestMethod.GET)
    public String doGetLists(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            PostsQuery postsQuery = new PostsQuery();

            postsQuery=  JSON.parseObject(urlParam,PostsQuery.class);

            List<PostsTable> postsList= postsDomain.getPostsList(postsQuery);

            return returnUtil.getSuccess(PostsJson.toJson(postsList));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }
}
